package groups

import (
	"GinTemplate/internal/models"
	"GinTemplate/internal/services/groups"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// Search
//
//	@Summary		查询群组
//	@Description	get groups
//	@Tags			groups
//	@Produce		application/json
//	@Accept			application/json
//	@Param			from	query	int false "from"
//	@Param			size	query	int	false "size"
//	@Router			/api/v1/groups [get]
//	@Success 200 {object} models.GenericData[models.GenericListData[models.Group]]
//	@Failure 400 {object} models.GenericResponse
//	@Failure 500 {object} models.GenericResponse
func Search(ctx *gin.Context) {
	fromStr := ctx.DefaultQuery("from", "0")
	from, err := strconv.Atoi(fromStr)
	if err != nil {
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{
			"message": "from is not int"})
	}
	sizeStr := ctx.DefaultQuery("size", "10")
	size, err := strconv.Atoi(sizeStr)
	if err != nil {
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{
			"message": "size is not int"})
	}

	gs, total, err := groups.Search(from, size)
	ctx.JSON(http.StatusOK, models.GenericData[models.GenericListData[models.Group]]{
		GenericResponse: models.GenericResponse{
			Code:    200,
			Message: "ok",
		},
		Data: models.GenericListData[models.Group]{
			Total: int(total),
			Items: gs,
		},
	})
}

// CreateOrUpdate
//
//	@Summary		创建或更新群组
//	@Description	id is empty, create; id is not empty, update
//	@Tags			groups
//	@Produce		application/json
//	@Accept			application/json
//	@Param			group	body	models.Group	true	"群组"
//	@Router			/api/v1/groups [post]
//	@Success 200 {object} models.GenericResponse
//	@Failure 400 {object} models.GenericResponse
//	@Failure 500 {object} models.GenericResponse
func CreateOrUpdate(ctx *gin.Context) {
	var body models.Group
	if err := ctx.BindJSON(&body); err != nil {
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{
			"message": "body is not valid"})
	}
	if body.ID == "" {
		err := groups.Create(body)
		if err != nil {
			ctx.JSON(http.StatusInternalServerError, gin.H{
				"message": "Internal error"})
		}
	} else {
		err := groups.Update(body.ID, models.UpdateGroup{
			Tag: body.Tag,
		})
		if err != nil {
			ctx.JSON(http.StatusInternalServerError, gin.H{
				"message": "Internal error"})
		}
	}
	ctx.JSON(http.StatusOK, gin.H{
		"message": "ok",
	})
}

// Update
//
//	@Summary		更新群组Label
//	@Description	update group by ID
//	@Tags			groups
//	@Produce		application/json
//	@Accept			application/json
//	@Param			id	path	int	true	"群组ID"
//	@Param			group	body	models.UpdateGroup	true	"更新内容"
//	@Router			/api/v1/groups/{id} [put]
//	@Success 200 {object} models.GenericResponse
//	@Failure 400 {object} models.GenericResponse
//	@Failure 500 {object} models.GenericResponse
func Update(ctx *gin.Context) {
	id := ctx.Param("id")
	var body models.UpdateGroup
	if err := ctx.BindJSON(&body); err != nil {
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{
			"message": "body is not valid"})
	}
	err := groups.Update(id, body)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"message": "Internal error"})
	}
}

// Delete
//
//	@Summary		删除群组
//	@Description	del group by ID
//	@Tags			groups
//	@Produce		application/json
//	@Param			id	path	int	true	"群组ID"
//	@Router			/api/v1/groups/{id} [delete]
//	@Success 200 {object} models.GenericResponse
//	@Failure 400 {object} models.GenericResponse
//	@Failure 500 {object} models.GenericResponse
func Delete(ctx *gin.Context) {
	id := ctx.Param("id")
	err := groups.Delete(id)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"message": "Internal Server Error!"})
	}
}
